package com.link2room.rodimus.facade.impl;

import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;

import com.link2room.rodimus.constant.CouponConstant;
import com.link2room.rodimus.constant.MallGoodsConstant;
import com.link2room.rodimus.constant.MallOrderConstant;
import com.link2room.rodimus.dto.param.inside.ExpressNewInsideParamDto;
import com.link2room.rodimus.dto.param.inside.MallOrderPayInsideParamDto;
import com.link2room.rodimus.entity.CouponDetailEntity;
import com.link2room.rodimus.entity.MallOrderBase;
import com.link2room.rodimus.entity.MallOrderPrice;
import com.link2room.rodimus.facade.IMallInsideFacadeService;
import com.link2room.rodimus.feign.rodimus.ExpressFeignService;
import com.link2room.rodimus.listener.event.MallOrderPaySuccessEvent;
import com.link2room.rodimus.service.ICouponService;
import com.link2room.rodimus.service.IGroupBuyingService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/21.
 */
@Service("mallInsideFacadeService")
public class MallInsideFacadeServiceImpl implements IMallInsideFacadeService {

    @Resource
    private ICommonService commonService;
    @Resource
    private ICouponService couponService;
    @Autowired
    private ExpressFeignService expressFeignService;
    @Resource
    private IGroupBuyingService groupBuyingService;
    @Autowired
    private ApplicationEventPublisher publisher;

    @Override
    public void NotifyOrderPay(MallOrderPayInsideParamDto mallOrderPayInsideParamDto) throws Exception {
        MallOrderBase mallOrderBaseFinder = new MallOrderBase();
        mallOrderBaseFinder.setLhotelGroupCode(mallOrderPayInsideParamDto.getLhotelGroupCode());
        mallOrderBaseFinder.setOrderCode(mallOrderPayInsideParamDto.getOrderNo());
        MallOrderBase mallOrderBase = commonService.findOneEQ(MallOrderBase.class, mallOrderBaseFinder);
        if (MallOrderConstant.Sta.needPay.equals(mallOrderBase.getSta())) {
            if (MallOrderConstant.ORDER_TYPE.GROUP_BUYING.equals(mallOrderBase.getOrderType())) {
                groupBuyingService.teamPaid(mallOrderBase);
            } else {
                mallOrderBase.setSta(MallOrderConstant.Sta.init);
                mallOrderBase.setMoneyPaySta(MallOrderConstant.MoneyPaySat.PAID);
                mallOrderBase.setMoneyPayType(mallOrderPayInsideParamDto.getPayType());
                mallOrderBase.setMoneyTaNo(mallOrderPayInsideParamDto.getTaNo());
                commonService.merge(mallOrderBase);
            }
        }
        //如果是券类型，则生成券
        if (MallGoodsConstant.GoodType.COUPON.equals(mallOrderBase.getGoodType())) {

            String orderCode = mallOrderBase.getOrderCode();
            MallOrderPrice find = new MallOrderPrice();
            find.setOrderCode(orderCode);
            QueryResult<MallOrderPrice> qres = commonService.findAllEQ(MallOrderPrice.class, find);
            if (qres != null && CollectionUtils.isNotEmpty(qres.getResultlist())) {
                String opneid = mallOrderBase.getOpenid();
                String memberNo = mallOrderBase.getMemberNo();
                List<CouponDetailEntity> newDetails = new ArrayList<>();
                qres.getResultlist().forEach(entity -> {
                    Integer count = entity.getCount();
                    String goodCode = entity.getGoodCode();
                    newDetails.addAll(couponService.createNewCouponDetail(mallOrderBase.getOrderCode(), mallOrderBase.getLhotelGroupCode(), goodCode, opneid, count, CouponConstant.SALE_TYPE.SHOP, memberNo));
                });
                commonService.saveList(newDetails);
            }

        } else if (MallGoodsConstant.GoodType.MG.equals(mallOrderBase.getGoodType())) { //发货
            if (BigDecimal.ZERO.compareTo(mallOrderBase.getPrice()) < 0){ //纯积分兑换的商品已经在订单生成是创建了发货信息
                //新建发货信息
                ExpressNewInsideParamDto expressParamDto = new ExpressNewInsideParamDto();
                expressParamDto.setMemberNo(mallOrderBase.getMemberNo());
                expressParamDto.setMobile(mallOrderBase.getExpressMobile());
                expressParamDto.setAddress(mallOrderBase.getExpressAddress());
                expressParamDto.setGoodAmount(mallOrderBase.getAmount());
                expressParamDto.setGoodName(mallOrderBase.getGoodContant());
                expressParamDto.setSourceNo(mallOrderBase.getOrderCode());
                expressParamDto.setReceiverName(mallOrderBase.getExpressMan());
                expressParamDto.setSourceType("MALL");
                expressParamDto.setRemark("商城订单");
                expressParamDto.setLhotelGroupCode(mallOrderBase.getLhotelGroupCode());
                expressParamDto.setOpenid(mallOrderBase.getOpenid());
                expressFeignService.newExpress(expressParamDto);
            }
        }

        publisher.publishEvent(new MallOrderPaySuccessEvent(mallOrderBase.getLhotelGroupCode(), mallOrderPayInsideParamDto.getOrderNo()));
    }
}
